package Presentation;

import BusinessLogic.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class frmClient extends javax.swing.JFrame {

    static frmLogin login = null;
    static frmClient client = null;
    static frmChat[] chat = null;
    static frmStateInfo[] sta = null;
    static frmAddFriend addfriend = null;
    static frmFindFriend findfriend = null;
    static frmChangePass changepass = null;
    static frmInvite invite=null;
    static frmGroupChat groupchat=null;
    private DefaultListModel modelSave = new DefaultListModel();
    private DefaultListModel modelShow = new DefaultListModel();
    private DefaultListModel modelGroup = new DefaultListModel();
    private DefaultListModel modelLock = new DefaultListModel();
    
    int numberfriend = 0;
    public static String rad = "online";

    public frmClient() {
        initComponents();
        searchListen();
        setIcon();
        this.setLocation(970, 100);
        this.setVisible(false);
        lst_Users.setModel(modelShow);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btg_MenuStatus = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        btg_RadioStatus = new javax.swing.ButtonGroup();
        txt_Search = new javax.swing.JTextField();
        lb_Hello = new javax.swing.JLabel();
        rad_Online = new javax.swing.JRadioButton();
        rad_Invisible = new javax.swing.JRadioButton();
        rad_Busy = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        sp_users = new javax.swing.JScrollPane();
        lst_Users = new javax.swing.JList();
        btn_Add = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lb_Username = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        MenuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mni_SendMessage = new javax.swing.JMenuItem();
        mni_FindFriend = new javax.swing.JMenuItem();
        mni_Users = new javax.swing.JMenu();
        mni_LockFriend = new javax.swing.JMenuItem();
        mni_UnlockFriend = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenu4 = new javax.swing.JMenu();
        mni_Online = new javax.swing.JCheckBoxMenuItem();
        mni_Invisible = new javax.swing.JCheckBoxMenuItem();
        mni_Busy = new javax.swing.JCheckBoxMenuItem();
        mni_ChangePass = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        mni_Logout = new javax.swing.JMenuItem();
        mni_Exit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu5 = new javax.swing.JMenu();
        mni_AddFriend = new javax.swing.JMenuItem();
        mni_DeleteFriend = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        mni_CreateGroup = new javax.swing.JMenuItem();
        mni_DeleteGroup = new javax.swing.JMenuItem();
        jMenu8 = new javax.swing.JMenu();
        mni_ShowFriend = new javax.swing.JCheckBoxMenuItem();
        jMenu3 = new javax.swing.JMenu();
        mni_Address = new javax.swing.JMenuItem();
        mni_Author = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chat_Application");
        setBackground(new java.awt.Color(0, 153, 153));
        setBounds(new java.awt.Rectangle(250, 300, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(0, 153, 153));
        setIconImages(getIconImages());
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        setName("frm_giaodienchinh"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        txt_Search.setFont(new java.awt.Font("Times New Roman", 2, 11)); // NOI18N
        txt_Search.setText("Nhập tên cần tìm");
        txt_Search.setName("txt_Search"); // NOI18N
        txt_Search.setPreferredSize(new java.awt.Dimension(87, 25));
        txt_Search.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txt_SearchMouseClicked(evt);
            }
        });
        txt_Search.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txt_SearchFocusLost(evt);
            }
        });

        lb_Hello.setBackground(new java.awt.Color(0, 102, 0));
        lb_Hello.setFont(new java.awt.Font("Times New Roman", 2, 12)); // NOI18N
        lb_Hello.setForeground(new java.awt.Color(153, 51, 0));
        lb_Hello.setText("       Chào bạn: ");

        btg_RadioStatus.add(rad_Online);
        rad_Online.setSelected(true);
        rad_Online.setText("Hiện");
        rad_Online.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        rad_Online.setName("rad_Online"); // NOI18N
        rad_Online.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rad_OnlineActionPerformed(evt);
            }
        });

        btg_RadioStatus.add(rad_Invisible);
        rad_Invisible.setText("Ẩn");
        rad_Invisible.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        rad_Invisible.setName("rad_Invisible"); // NOI18N
        rad_Invisible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rad_InvisibleActionPerformed(evt);
            }
        });

        btg_RadioStatus.add(rad_Busy);
        rad_Busy.setText("Bận");
        rad_Busy.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        rad_Busy.setName("rad_Busy"); // NOI18N
        rad_Busy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rad_BusyActionPerformed(evt);
            }
        });

        lst_Users.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        lst_Users.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        lst_Users.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        lst_Users.setName("ls_user"); // NOI18N
        lst_Users.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lst_UsersMouseClicked(evt);
            }
        });
        lst_Users.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lst_UsersValueChanged(evt);
            }
        });
        sp_users.setViewportView(lst_Users);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(sp_users)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(sp_users, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 339, Short.MAX_VALUE)
        );

        btn_Add.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add.png"))); // NOI18N
        btn_Add.setToolTipText("Thêm bạn");
        btn_Add.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Add.setName("btn_them"); // NOI18N
        btn_Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_AddActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 51, 204));
        jLabel2.setText("Thêm bạn");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 0, 0));
        jLabel4.setText("G_IT");

        lb_Username.setBackground(new java.awt.Color(0, 102, 0));
        lb_Username.setFont(new java.awt.Font("Times New Roman", 2, 12)); // NOI18N
        lb_Username.setForeground(new java.awt.Color(153, 51, 0));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search.png"))); // NOI18N

        MenuBar.setName("menubar"); // NOI18N

        jMenu1.setText("Tùy chọn");
        jMenu1.setName("menu_tuychon"); // NOI18N
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        mni_SendMessage.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        mni_SendMessage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/send_message.png"))); // NOI18N
        mni_SendMessage.setText("Gửi tin nhắn");
        mni_SendMessage.setName("mn_guitn"); // NOI18N
        mni_SendMessage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_SendMessageActionPerformed(evt);
            }
        });
        jMenu1.add(mni_SendMessage);

        mni_FindFriend.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        mni_FindFriend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/find_friend.png"))); // NOI18N
        mni_FindFriend.setText("Tìm bạn");
        mni_FindFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_FindFriendActionPerformed(evt);
            }
        });
        jMenu1.add(mni_FindFriend);

        mni_Users.setText("Người dùng");

        mni_LockFriend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/lock_go.png"))); // NOI18N
        mni_LockFriend.setText("Khóa người dùng");
        mni_LockFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_LockFriendActionPerformed(evt);
            }
        });
        mni_Users.add(mni_LockFriend);

        mni_UnlockFriend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/lock_open.png"))); // NOI18N
        mni_UnlockFriend.setText("Mở khóa người dùng");
        mni_UnlockFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_UnlockFriendActionPerformed(evt);
            }
        });
        mni_Users.add(mni_UnlockFriend);

        jMenu1.add(mni_Users);
        jMenu1.add(jSeparator1);

        jMenu4.setText("Đổi trạng thái");
        jMenu4.setName("mn_doitt"); // NOI18N

        btg_MenuStatus.add(mni_Online);
        mni_Online.setSelected(true);
        mni_Online.setText("Hiện");
        mni_Online.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/status_online.png"))); // NOI18N
        mni_Online.setName("mni_Online"); // NOI18N
        mni_Online.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_OnlineActionPerformed(evt);
            }
        });
        jMenu4.add(mni_Online);

        btg_MenuStatus.add(mni_Invisible);
        mni_Invisible.setText("Ẩn");
        mni_Invisible.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/status_offline.png"))); // NOI18N
        mni_Invisible.setName("mni_Invisible"); // NOI18N
        mni_Invisible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_InvisibleActionPerformed(evt);
            }
        });
        jMenu4.add(mni_Invisible);

        btg_MenuStatus.add(mni_Busy);
        mni_Busy.setText("Bận");
        mni_Busy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/status_busy.png"))); // NOI18N
        mni_Busy.setName("mni_Busy"); // NOI18N
        mni_Busy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_BusyActionPerformed(evt);
            }
        });
        jMenu4.add(mni_Busy);

        jMenu1.add(jMenu4);

        mni_ChangePass.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.SHIFT_MASK));
        mni_ChangePass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/key (2).png"))); // NOI18N
        mni_ChangePass.setText("Đổi mật khẩu");
        mni_ChangePass.setName("mn_doimk"); // NOI18N
        mni_ChangePass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_ChangePassActionPerformed(evt);
            }
        });
        jMenu1.add(mni_ChangePass);
        jMenu1.add(jSeparator2);

        mni_Logout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        mni_Logout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/lock.png"))); // NOI18N
        mni_Logout.setText("Đăng xuất");
        mni_Logout.setName("mn_dangx"); // NOI18N
        mni_Logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_LogoutActionPerformed(evt);
            }
        });
        jMenu1.add(mni_Logout);

        mni_Exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        mni_Exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/exit.png"))); // NOI18N
        mni_Exit.setText("Thoát");
        mni_Exit.setName("mn_thoat"); // NOI18N
        mni_Exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_ExitActionPerformed(evt);
            }
        });
        jMenu1.add(mni_Exit);

        MenuBar.add(jMenu1);

        jMenu2.setText("Quản lý");
        jMenu2.setName("menu_quanly"); // NOI18N

        jMenu5.setText("QL danh bạ");
        jMenu5.setName("mn_qldanhba"); // NOI18N

        mni_AddFriend.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        mni_AddFriend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add (2).png"))); // NOI18N
        mni_AddFriend.setText("Thêm bạn");
        mni_AddFriend.setName("mni_AddFriend"); // NOI18N
        mni_AddFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_AddFriendActionPerformed(evt);
            }
        });
        jMenu5.add(mni_AddFriend);

        mni_DeleteFriend.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        mni_DeleteFriend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete.png"))); // NOI18N
        mni_DeleteFriend.setText("Xóa bạn");
        mni_DeleteFriend.setName("mni_DeleteFriend"); // NOI18N
        mni_DeleteFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_DeleteFriendActionPerformed(evt);
            }
        });
        jMenu5.add(mni_DeleteFriend);

        jMenu2.add(jMenu5);

        jMenu6.setText("QL nhóm");
        jMenu6.setName("mn_qlnhom"); // NOI18N

        mni_CreateGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/group_add.png"))); // NOI18N
        mni_CreateGroup.setText("Tạo nhóm mới");
        mni_CreateGroup.setName("mni_taonhom"); // NOI18N
        mni_CreateGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_CreateGroupActionPerformed(evt);
            }
        });
        jMenu6.add(mni_CreateGroup);

        mni_DeleteGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/group_delete.png"))); // NOI18N
        mni_DeleteGroup.setText("Xóa nhóm");
        mni_DeleteGroup.setName("mni_DeleteGroup"); // NOI18N
        mni_DeleteGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_DeleteGroupActionPerformed(evt);
            }
        });
        jMenu6.add(mni_DeleteGroup);

        jMenu2.add(jMenu6);

        jMenu8.setText("Ẩn/Hiện");
        jMenu8.setName("mnan_hien"); // NOI18N

        mni_ShowFriend.setText("Hiện những bạn online");
        mni_ShowFriend.setName("mni_ShowFriend"); // NOI18N
        mni_ShowFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_ShowFriendActionPerformed(evt);
            }
        });
        jMenu8.add(mni_ShowFriend);

        jMenu2.add(jMenu8);

        MenuBar.add(jMenu2);

        jMenu3.setText("Trợ giúp");
        jMenu3.setName("menu_trogiup"); // NOI18N

        mni_Address.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.ALT_MASK));
        mni_Address.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/help.png"))); // NOI18N
        mni_Address.setText("Chương trình");
        mni_Address.setName("mni_Address"); // NOI18N
        mni_Address.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_AddressActionPerformed(evt);
            }
        });
        jMenu3.add(mni_Address);

        mni_Author.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_H, java.awt.event.InputEvent.CTRL_MASK));
        mni_Author.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/house.png"))); // NOI18N
        mni_Author.setText("Tác giả");
        mni_Author.setName("mni_Author"); // NOI18N
        mni_Author.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mni_AuthorActionPerformed(evt);
            }
        });
        jMenu3.add(mni_Author);

        MenuBar.add(jMenu3);

        setJMenuBar(MenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txt_Search, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(rad_Online)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rad_Invisible)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rad_Busy))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lb_Hello)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lb_Username, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btn_Add, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addGap(38, 38, 38))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lb_Hello, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lb_Username, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rad_Online)
                    .addComponent(rad_Invisible)
                    .addComponent(rad_Busy))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txt_Search, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(jLabel2))
                    .addComponent(btn_Add, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void txt_SearchMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txt_SearchMouseClicked
        txt_Search.selectAll();
        txt_Search.requestFocus();
    }//GEN-LAST:event_txt_SearchMouseClicked
    private void lst_UsersValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lst_UsersValueChanged

    }//GEN-LAST:event_lst_UsersValueChanged
    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        stateUser();
    }//GEN-LAST:event_formWindowOpened
    private void btn_AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_AddActionPerformed
        addfriend = new frmAddFriend();
        addfriend.setVisible(true);
        addfriend.setUsername(lb_Username.getText());
    }//GEN-LAST:event_btn_AddActionPerformed
    private void rad_OnlineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rad_OnlineActionPerformed
        if (!rad.equals("online")) {
            mni_Online.setSelected(true);
            rad = "online";
            sendData(Command.SEND_STATE + Command.ONLINE + lb_Username.getText());
            stateUser();

        }
    }//GEN-LAST:event_rad_OnlineActionPerformed
    private void rad_InvisibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rad_InvisibleActionPerformed
        if (!rad.equals("invisible")) {
            mni_Invisible.setSelected(true);
            rad = "invisible";
            sendData(Command.SEND_STATE + Command.INVISIBLE + lb_Username.getText());
            stateUser();
        }
    }//GEN-LAST:event_rad_InvisibleActionPerformed
    private void rad_BusyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rad_BusyActionPerformed
        if (!rad.equals("busy")) {
            mni_Busy.setSelected(true);
            rad = "busy";
            sendData(Command.SEND_STATE + Command.BUSY + lb_Username.getText());
            stateUser();
        }

    }//GEN-LAST:event_rad_BusyActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    try {
        Connection.exitProgram();
    } catch (IOException ex) {
    }
}//GEN-LAST:event_formWindowClosing

private void mni_SendMessageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_SendMessageActionPerformed
    String namechat;
    namechat = (String) JOptionPane.showInputDialog(null, "Chọn tên người dùng từ danh sách:", "Chat",
            JOptionPane.PLAIN_MESSAGE, null, getFriendList(), "");
    if (!namechat.equals("")) {
        sendData(Command.CHAT_FRIEND_REQUEST + lb_Username.getText() + ((char) 001) + namechat);
    } else {
        JOptionPane.showMessageDialog(null, "Tên tài khoản không được để trống", "Thông báo!", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_mni_SendMessageActionPerformed

private void mni_LockFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_LockFriendActionPerformed
    String namelock;
    namelock = (String) JOptionPane.showInputDialog(null, "Chọn tên người dùng từ danh sách:", "Khóa người dùng",
            JOptionPane.PLAIN_MESSAGE, null, getFriendList(), "");
    if (!namelock.equals("")) {
        Connection.sendData(Command.LOCK_FRIEND_REQUEST + lb_Username.getText() + ((char) 001) + namelock);
    } else {
        JOptionPane.showMessageDialog(null, "Tên tài khoản không được để trống", "Thông báo!", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_mni_LockFriendActionPerformed

private void mni_UnlockFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_UnlockFriendActionPerformed
    String nameunlock;
    nameunlock = (String) JOptionPane.showInputDialog(null, "Chọn tên người dùng từ danh sách:", "Mở khóa người dùng",
            JOptionPane.PLAIN_MESSAGE, null, getFriendList(), "");
    if (!nameunlock.equals("")) {
        Connection.sendData(Command.UNLOCK_FRIEND_REQUEST + lb_Username.getText() + ((char) 001) + nameunlock);
    } else {
        JOptionPane.showMessageDialog(null, "Tên tài khoản không được để trống", "Thông báo!", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_mni_UnlockFriendActionPerformed

private void mni_OnlineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_OnlineActionPerformed
    if (!rad.equals("online")) {
        rad_Online.setSelected(true);
        rad = "online";
        sendData(Command.SEND_STATE + Command.ONLINE + lb_Username.getText());
        stateUser();
    }
}//GEN-LAST:event_mni_OnlineActionPerformed

private void mni_InvisibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_InvisibleActionPerformed
    if (!rad.equals("invisible")) {
        rad_Invisible.setSelected(true);
        rad = "invisible";
        sendData(Command.SEND_STATE + Command.INVISIBLE + lb_Username.getText());
        stateUser();
    }
}//GEN-LAST:event_mni_InvisibleActionPerformed

private void mni_BusyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_BusyActionPerformed
    if (!rad.equals("busy")) {
        rad_Busy.setSelected(true);
        rad = "busy";
        sendData(Command.SEND_STATE + Command.BUSY + lb_Username.getText());
        stateUser();
    }

}//GEN-LAST:event_mni_BusyActionPerformed

private void mni_ChangePassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_ChangePassActionPerformed
    changepass = new frmChangePass();
    changepass.setVisible(true);
    changepass.username = lb_Username.getText();
}//GEN-LAST:event_mni_ChangePassActionPerformed

private void mni_LogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_LogoutActionPerformed
    try {
        Connection.logout();
        this.setVisible(false);
        client = new frmClient();
        login = new frmLogin();
        login.setVisible(true);
    } catch (IOException ex) {
    }
}//GEN-LAST:event_mni_LogoutActionPerformed

private void mni_ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_ExitActionPerformed
    System.exit(0);
}//GEN-LAST:event_mni_ExitActionPerformed

private void mni_AddFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_AddFriendActionPerformed
    addfriend = new frmAddFriend();
    addfriend.setVisible(true);
    addfriend.setUsername(lb_Username.getText());
}//GEN-LAST:event_mni_AddFriendActionPerformed

private void mni_DeleteFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_DeleteFriendActionPerformed
    String NameDelete;
    NameDelete = (String) JOptionPane.showInputDialog(null, "Chọn tên người dùng từ danh sách:", "Xóa người dùng",
            JOptionPane.PLAIN_MESSAGE, null, getFriendList(), "");
    if (!NameDelete.equals("")) {
        Connection.sendData(Command.DELETE_FRIEND_REQUEST + lb_Username.getText() + ((char) 001) + NameDelete);
    } else {
        JOptionPane.showMessageDialog(null, "Tên tài khoản không được để trống", "Thông báo!", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_mni_DeleteFriendActionPerformed

private void mni_AddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_AddressActionPerformed
//    try {
//        Runtime.getRuntime().exec("hh.exe "+ System.getProperty("user.dir") + "/help.chm" );
//    } catch (IOException ex) {
//        ex.printStackTrace();
//    }
}//GEN-LAST:event_mni_AddressActionPerformed

private void mni_AuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_AuthorActionPerformed
    JOptionPane.showMessageDialog(this, "-  Trường Đại Học Cần Thơ - Khoa CNTT&TT\n- Đồ án Niên Luận Phần Mềm\n - Lớp: DI1096A3\n- Tác giả:\n"
            + "\t   . Lâm Thành Trung 1101706\n"
            + "\t   . Huỳnh Minh Thuận 1101693\n"
            + "\t   . Lê Nguyễn Thông 1101692\n"
            + "\t   . Lục Văn Quí 1101674\n"
            + "- Version: 1.0");
}//GEN-LAST:event_mni_AuthorActionPerformed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed

    }//GEN-LAST:event_jMenu1ActionPerformed

    private void mni_FindFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_FindFriendActionPerformed
        findfriend = new frmFindFriend();
        findfriend.setVisible(true);
        findfriend.name = getUsername();
    }//GEN-LAST:event_mni_FindFriendActionPerformed

    private void lst_UsersMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lst_UsersMouseClicked
        if (evt.getClickCount() == 2) {
            try {
                sendData(Command.CHAT_FRIEND_REQUEST + lb_Username.getText() + ((char) 001) + getFriendname(lst_Users.getSelectedValue().toString()));
            } catch (HeadlessException e) {
            }
        }
    }//GEN-LAST:event_lst_UsersMouseClicked

    private void txt_SearchFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txt_SearchFocusLost
        //txt_Search.setText("Nhập tên cần tìm");
    }//GEN-LAST:event_txt_SearchFocusLost

    private void mni_ShowFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_ShowFriendActionPerformed
        if (mni_ShowFriend.isSelected()) {
            int n = 0;
            while (n < modelShow.size()) {
                int st = getStateUser(modelShow.getElementAt(n).toString());
                if (st != 0 && st != 5) {
                    n++;
                } else {
                    modelShow.removeElementAt(n);
                }
            }
        } else {
            updateFriendList();
        }
    }//GEN-LAST:event_mni_ShowFriendActionPerformed

    private void mni_CreateGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_CreateGroupActionPerformed
        String group = "";
        try {
            group = JOptionPane.showInputDialog(client, "Nhập tên nhóm cần tạo", "Tạo nhóm mới", JOptionPane.PLAIN_MESSAGE);
            if (!group.equals("")) {
                Connection.sendData(Command.CREATE_GROUP + lb_Username.getText() + ((char) 001) + group);
            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_mni_CreateGroupActionPerformed

    private void mni_DeleteGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mni_DeleteGroupActionPerformed
        Object[] option = {"Đồng ý", "Hủy bỏ"};
        int dialogResult = JOptionPane.showOptionDialog(this, "Bạn có chắc chắn muốn xóa nhóm đã tạo?", "",JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,null,option,option[0]);
        if(dialogResult==0){
            Connection.sendData(Command.REMOVE_GROUP+lb_Username.getText());
        }
    }//GEN-LAST:event_mni_DeleteGroupActionPerformed

    private void stateUser() {
        if (rad.equals("invisible")) {
            lb_Username.setIcon(new ImageIcon(getClass().getResource("/Images/status_offline.png")));
        }
        if (rad.equals("online")) {
            lb_Username.setIcon(new ImageIcon(getClass().getResource("/Images/status_online.png")));
        }
        if (rad.equals("busy")) {
            lb_Username.setIcon(new ImageIcon(getClass().getResource("/Images/status_busy.png")));
        }
    }

    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/icon.png")));
    }

    //Hiển thị danh sách bạn
    private void loadFriendList(String fl) {
        int spos = 0, epos = -1;
        String friend;
        for (int i = 0; i < fl.length(); i++) {
            if (fl.charAt(i) == 001) {
                spos = epos + 1;
                epos = i;
                friend = new String(fl.substring(spos, epos));
                modelSave.addElement(friend);
                modelShow.addElement(friend);
            }
        }
        StatusImage.Image(lst_Users);
        numberfriend = modelSave.getSize();
        chat = new frmChat[numberfriend + 5]; //Khởi tạo chat
        sta = new frmStateInfo[numberfriend + 5];

        for (int j = 0; j < (numberfriend + 5); j++) {
            chat[j] = new frmChat(lb_Username.getText());
            sta[j] = new frmStateInfo();
        }
    }

    //Lấy tên đăng nhập
    public String getUsername() {
        return this.lb_Username.getText();
    }

    //Lấy tên bạn trong danh sách
    public String getFriendname(String str) {
        return str.substring(1, str.length());
    }

    //Lấy trạng thái của user
    private int getStateUser(String str) {
        return Integer.parseInt(str.substring(0, 1));
    }

    //Đặt tên đăng nhập
    private void setUsername(String str) {
        lb_Username.setText(str);
    }

    //Xác định số thứ tự bạn trong danh sách lưu trữ
    private int getID(String fri) {
        for (int i = 0; i < modelSave.getSize(); i++) {
            if (getFriendname(modelSave.getElementAt(i).toString()).equals(fri)) {
                return i;
            }
        }
        return -1;
    }

    //Xác định số thứ tự bạn trong danh sách hiển thị
    private int getIDShow(String fri) {
        for (int i = 0; i < modelShow.getSize(); i++) {
            if (getFriendname(modelShow.getElementAt(i).toString()).equals(fri)) {
                return i;
            }
        }
        return -1;
    }

    //Cập nhật lại danh sách
    public void updateFriendList() {
        modelShow.clear();
        for (int j = 0; j < modelSave.size(); j++) {
            int st = getStateUser(modelSave.getElementAt(j).toString());
            if (st != 5) {
                modelShow.addElement(modelSave.getElementAt(j).toString());
            }
        }
    }

    //Lấy danh sách bạn
    private Object[] getFriendList() {

        ArrayList<String> lst = new ArrayList();
        for (int i = 0; i < modelSave.getSize(); i++) {
            if (getStateUser(modelSave.getElementAt(i).toString()) != 5) {
                lst.add(getFriendname(modelSave.getElementAt(i).toString()));
            }
        }
        Object[] list = new Object[lst.size()];
        int j = 0;
        for (String a : lst) {
            list[j] = a;
            j++;
        }
        return list;
    }

    //Lấy danh sách bạn kèm theo trạng thái
    public Object[] getFriendList_State(){
        Object[] list;
        list = new Object[modelShow.getSize()];
        for(int i=0;i<modelShow.getSize();i++){
                list[i]=modelShow.getElementAt(i).toString();
        }
        return list;
    }
    
    public DefaultListModel getModelGroup(){
        return modelGroup;
    }

    //Xóa bạn
    private void deleteFriend(String str) {
        if (getIDShow(str) != -1) {
            modelShow.removeElementAt(getIDShow(str));
        }
        modelSave.setElementAt("5" + str, getID(str));
    }

    private void sendData(String str) {
        if (!Connection.sendData(str)) {
            JOptionPane.showMessageDialog(rootPane, "Lỗi! Mất kết nối với server", "Lỗi kết nối!", JOptionPane.ERROR_MESSAGE);
            mni_LogoutActionPerformed(null);
        }
    }

    private void updateFriendState(String friend, String state) {
        String newvalue = state + getFriendname(modelSave.getElementAt(getID(friend)).toString());
        modelSave.setElementAt(newvalue, getID(friend));
        if (getIDShow(friend) != -1) {
            modelShow.setElementAt(newvalue, getIDShow(friend));
        }
        StatusImage.Image(client.lst_Users);
    }
    //mở khóa 1 người
    private void unLock(String fl){
        for(int i=0;i<modelLock.getSize();i++){
            if(fl.equals(modelLock.getElementAt(i).toString()))
                modelLock.removeElementAt(i);
        }
    }
    //kiểm tra user đó có bị lock hay khong?
    private boolean checkLockUser(String name){
        for(int i=0;i<modelLock.getSize();i++){
            if(name.equals(modelLock.getElementAt(i).toString()))
                return true;
        }
        return false;
    }
    //cập nhật modelLock
    public void updateModelLock(String fl){
        int spos = 0, epos = -1;
        String friend;
        for (int i = 0; i < fl.length(); i++) {
            if (fl.charAt(i) == 001) {
                spos = epos + 1;
                epos = i;
                friend = new String(fl.substring(spos, epos));
                modelLock.addElement(friend);
            }
        }
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                try {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(frmClient.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(frmClient.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(frmClient.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(frmClient.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
            }
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                client = new frmClient();
                login = new frmLogin();
                login.setVisible(true);
            }
        });
    }

    //Xử lý dữ liệu từ server gửi đến
    public static void processing(String str) {
        String name, friend, state;
        int id, idg;
        String command = new String(str.substring(0, 3));
        int sep = str.indexOf((char) 001);
        int sep1 = str.indexOf((char) 002);
        int intCommand = Integer.parseInt(command);
        switch (intCommand) {

            //Cho phép đăng nhập
            // <editor-fold defaultstate="collapsed" desc="LOGIN_SUCCESS">
            case (Command.LOGIN_SUCCESS):
                String State = str.substring(3, 4);
                name = str.substring(4, sep);
                String friendlist = str.substring(sep + 1, sep1);
                String lslock = str.substring(sep1+1, str.length());
                client.updateModelLock(lslock);
                login.setVisible(false);
                client.setVisible(true);
                client.setUsername(name);
                client.loadFriendList(friendlist);
                if (State.equals(Command.OFFLINE)) {
                    client.rad_Invisible.setSelected(true);
                } else {
                    client.sendData(Command.SEND_STATE + State + name);
                }
                break;
            // </editor-fold>

            //Đăng nhập sai
            // <editor-fold defaultstate="collapsed" desc="LOGIN_FAIL">
            case (Command.LOGIN_FAIL):
                JOptionPane.showMessageDialog(login, "Tên đăng nhập hoặc mật khẩu không đúng", "Thông báo", JOptionPane.WARNING_MESSAGE);
                break;
            // </editor-fold>

            //Trạng thái của bạn bè
            // <editor-fold defaultstate="collapsed" desc="FRIEND_STATE">
            case (Command.FRIEND_STATE):
                state = str.substring(3, 4);
                name = str.substring(4, sep);
                friend = str.substring(sep + 1, str.length());
                id = client.getID(friend);           
                client.updateFriendState(friend, state);
                chat[id].setFriend(friend,client.getStateUser(client.modelSave.getElementAt(id).toString()));
                sta[id].setUsername(name);
                sta[id].setVisible(true);
                sta[id].notice(state, friend);
                break;
            // </editor-fold>

            //Hiển thị tin nhắn offline
            // <editor-fold defaultstate="collapsed" desc="MESSAGE_OFFLINE">
            case (Command.MESSAGE_OFFLINE):
                friend = str.substring(3, sep);
                name = str.substring(sep + 1, sep1);
                String message = str.substring(sep1 + 1, str.length());
                id = client.getID(friend);
                int count = chat[id].getCount();

                chat[id].setVisible(true);
                String style = "0000000";
                chat[id].friendMessage(friend, style, message);
                chat[id].setWatch(chat[id].getWatch() + 1);
                int watch = chat[id].getWatch();
                if (watch == count) {
                    client.sendData(Command.IS_WATCHED_MESSAGE_OFFLINE + name + ((char) 001) + friend);
                }
                System.out.println(name + " " + friend);
                break;
            // </editor-fold>  

            //Đăng ký thành công    
            // <editor-fold defaultstate="collapsed" desc="REGISTER_SUCCESS">
            case (Command.REGISTER_SUCCESS):
                name = str.substring(3, str.length());
                JOptionPane.showMessageDialog(null, "Đăng kí tài khoản thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                frmLogin.closeRegister();
                break;
            // </editor-fold>

            //Đăng ký thất bại
            //<editor-fold defaultstate="collapsed" desc="REGISTER_FAIL">
            case (Command.REGISTER_FAIL):
                JOptionPane.showMessageDialog(null, "Tài khoản đã tồn tại", "Thông báo", JOptionPane.WARNING_MESSAGE);
                break;
            //</editor-fold>

            //Yêu cầu thêm bạn
            //<editor-fold defaultstate="collapsed" desc="ADD_FRIEND_REQUEST">
            case (Command.ADD_FRIEND_REQUEST):
                state = str.substring(3, 4);
                name = str.substring(4, sep);
                friend = str.substring(sep + 1, str.length());
                //Hiện thông báo kết bạn
                int dialogButton = JOptionPane.YES_NO_OPTION;
                Object[] options = {"Đồng ý", "Từ chối"};
                int dialogResult = JOptionPane.showOptionDialog(client, "Bạn có yêu cầu kết bạn từ " + friend, "Yêu cầu kết bạn", dialogButton,
                        JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
                if (dialogResult == 0) {
                    client.sendData(Command.ADD_FRIEND_ACCEPT + name + ((char) 001) + friend);
                    //cập nhật lại danh sách bạn
                    if (client.getID(friend) == -1) {
                        client.modelSave.addElement(state + friend);
                    } else {
                        client.updateFriendState(friend, state);
                    }
                    client.modelShow.addElement(state + friend);
                } else {
                    client.sendData(Command.ADD_FRIEND_REFUSE + name + ((char) 001) + friend);
                }
                break;
            //</editor-fold>

            //Thêm bạn thành công
            //<editor-fold defaultstate="collapsed" desc="ADD_FRIEND_SUCCESS">
            case (Command.ADD_FRIEND_SUCCESS):
                state = str.substring(3, 4);
                friend = str.substring(4, str.length());
                //cập nhật lại danh sách bạn
                if (client.getID(friend) == -1) {
                    client.modelSave.addElement(state + friend);
                } else {
                    client.updateFriendState(friend, state);
                }
                client.modelShow.addElement(state + friend);
                JOptionPane.showMessageDialog(client, friend + " đã đồng ý kết bạn", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>

            //Thêm bạn thất bại
            //<editor-fold defaultstate="collapsed" desc="ADD_FRIEND_FAIL">
            case (Command.ADD_FRIEND_FAIL):
                friend = str.substring(3, str.length());
                JOptionPane.showMessageDialog(client, friend + " đã từ chối kết bạn", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>

            //Đã là bạn
            //<editor-fold defaultstate="collapsed" desc="ADD_ALREADY_FRIEND">
            case (Command.ADD_ALREADY_FRIEND):
                JOptionPane.showMessageDialog(client, "Đã là bạn từ trước", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                break;
                //</editor-fold>

            //Tên nhập vào không tồn tại
            //<editor-fold defaultstate="collapsed" desc="ADD_NO_EXIST">
            case (Command.ADD_NO_EXIST):
                JOptionPane.showMessageDialog(client, "Tên nhập vào không tồn tại", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>

            //Đã gửi yêu cầu thêm bạn 
            //<editor-fold defaultstate="collapsed" desc="ADD_FRIEND_WAIT">
            case (Command.ADD_FRIEND_WAIT):
                friend = str.substring(3, str.length());
                JOptionPane.showMessageDialog(client, "Đã gửi yêu cầu kết bạn đến " + friend, "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>

            //Yêu cầu chat thành công
            //<editor-fold defaultstate="collapsed" desc="CHAT_FRIEND_SUCCESS">
            case (Command.CHAT_FRIEND_SUCCESS):
                name = str.substring(3, sep);
                friend = str.substring(sep + 1, str.length());
                id = client.getID(friend);
                chat[id].setVisible(true);
                chat[id].setFriend(friend,client.getStateUser(client.modelSave.getElementAt(id).toString()));
                chat[id].setUsername(name);
                chat[id].checkLock(client.checkLockUser(friend));
                break;
            //</editor-fold>

            //Yêu cầu chat thất bại
            //<editor-fold defaultstate="collapsed" desc="CHAT_REQUEST_FAIL">
            case (Command.CHAT_REQUEST_FAIL):
                friend = str.substring(3, str.length());
                JOptionPane.showMessageDialog(client, "Không thể chat với " + friend);
                break;
            //</editor-fold>

            //Khởi tạo chat khi được yêu cầu
            //<editor-fold defaultstate="collapsed" desc="CHAT_OPEN">
            case (Command.CHAT_OPEN):
                name = str.substring(3, sep);
                friend = str.substring(sep + 1, sep1);
                int n = Integer.parseInt(str.substring(sep1 + 1, str.length()));
                id = client.getID(friend);
                chat[id].setCount(n);
                chat[id].setWatch(0);
                chat[id].setFriend(friend,client.getStateUser(client.modelSave.getElementAt(id).toString()));
                chat[id].checkLock(client.checkLockUser(friend));
                chat[id].setUsername(name);
                break;
            //</editor-fold>

            //Hiện danh sách kết quả tìm bạn
            //<editor-fold defaultstate="collapsed" desc="FIND_LIST">
            case (Command.FIND_LIST):
                String list = str.substring(3, str.length());
                if (list.equals("")) {
                    findfriend.setNotify();
                } else {
                    findfriend.listFind(list);
                }
                break;
            //</editor-fold>

            //Lỗi tìm kiếm bạn
            //<editor-fold defaultstate="collapsed" desc="FIND_FAIL">
            case (Command.FIND_FAIL):
                JOptionPane.showMessageDialog(findfriend, "Lỗi tìm kiếm! Vui lòng thử lại", "Thông báo", JOptionPane.ERROR_MESSAGE);
                break;
            //</editor-fold>

            //Hiển thị tin nhắn nhận được
            //<editor-fold defaultstate="collapsed" desc="RECIEVED_MESSAGE">
            case (Command.RECIEVED_MESSAGE):
                style = str.substring(3, 10);
                name = str.substring(10, sep);
                friend = str.substring(sep + 1, sep1);
                String msg = str.substring(sep1 + 1, str.length());
                id = client.getID(friend);
                chat[id].setVisible(true);
                chat[id].friendMessage(friend, style, msg);
                break;
            //</editor-fold>

            //Nhận được buzz
            // <editor-fold defaultstate="collapsed" desc="BUZZ">
            case (Command.SEND_BUZZ):
                name = str.substring(3, sep);
                friend = str.substring(sep + 1, str.length());
                id = client.getID(friend);
                if (!chat[id].isVisible()) {
                    chat[id].setVisible(true);
                }
                chat[id].alert();
                break;
            // </editor-fold>

            // Người yêu cầu đã bị khóa rồi
            //<editor-fold defaultstate="collapsed" desc="LOCK_ALDREADY">
            case (Command.LOCK_ALDREADY):
                friend = str.substring(sep + 1, str.length());
                JOptionPane.showMessageDialog(client, friend + " đã bị khóa từ trước", "Thông báo khóa người dùng", JOptionPane.WARNING_MESSAGE);
                break;
            //</editor-fold>

            // Khóa thành công
            //<editor-fold defaultstate="collapsed" desc="LOCK_SUCCESS">
            case (Command.LOCK_SUCCESS):
                friend = str.substring(sep + 1, str.length());
                client.modelLock.addElement(friend);
                id = client.getID(friend);
                chat[id].checkLock(client.checkLockUser(friend));
                JOptionPane.showMessageDialog(client, "Khóa thành công: " + friend, "Thông báo khóa người dùng", JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>

            // Khóa thất bại
            //<editor-fold defaultstate="collapsed" desc="LOCK_FAIL">
            case (Command.LOCK_FAIL):
                JOptionPane.showMessageDialog(client, "Khóa thất bại", "Thông báo khóa người dùng", JOptionPane.ERROR_MESSAGE);
                break;
            //</editor-fold>

            // Khóa người không phải bạn
            //<editor-fold defaultstate="collapsed" desc="LOCK_NOT_FRIEND">
            case (Command.LOCK_NOT_FRIEND):
                friend = str.substring(sep + 1, str.length());
                JOptionPane.showMessageDialog(client, friend + " không phải là bạn", "Thông báo khóa người dùng", JOptionPane.WARNING_MESSAGE);
                break;
            //</editor-fold>

            // Người yêu cầu mở khóa không bị khóa    
            //<editor-fold defaultstate="collapsed" desc="UNLOCK_NOT_LOCK">
            case (Command.UNLOCK_NOT_LOCK):
                friend = str.substring(sep + 1, str.length());
                JOptionPane.showMessageDialog(client, friend + " không bị khóa từ trước", "Thông báo mở khóa người dùng", JOptionPane.WARNING_MESSAGE);
                break;
            //</editor-fold>

            //Mở khóa thành công
            //<editor-fold defaultstate="collapsed" desc="UNLOCK_SUCCESS">
            case (Command.UNLOCK_SUCCESS):
                friend = str.substring(sep + 1, str.length());
                client.unLock(friend);
                id = client.getID(friend);
                chat[id].checkLock(client.checkLockUser(friend));
                JOptionPane.showMessageDialog(client, "Mở khóa thành công: " + friend, "Thông báo mở khóa người dùng", JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>

            //Mở khóa thất bại
            //<editor-fold defaultstate="collapsed" desc="UNLOCK_FAIL">
            case (Command.UNLOCK_FAIL):
                JOptionPane.showMessageDialog(client, "Mở khóa thất bại", "Thông báo mở khóa người dùng", JOptionPane.ERROR_MESSAGE);
                break;
            //</editor-fold>

            // Mở khóa người không phải bạn
            //<editor-fold defaultstate="collapsed" desc="UNLOCK_NOT_FRIEND">
            case (Command.UNLOCK_NOT_FRIEND):
                friend = str.substring(sep + 1, str.length());
                JOptionPane.showMessageDialog(client, friend + " không phải là bạn", "Thông báo mở khóa người dùng", JOptionPane.WARNING_MESSAGE);
                break;
            //</editor-fold>

            // Yêu cầu đổi mật khẩu thành công
            //<editor-fold defaultstate="collapsed" desc="CHANGE_PASS_SUCCESS">
            case (Command.CHANGE_PASS_SUCCESS):
                JOptionPane.showMessageDialog(changepass, "Thay đổi mật khẩu thành công", "Thông báo đổi password", JOptionPane.INFORMATION_MESSAGE);
                changepass.setVisible(false);
                break;
            //</editor-fold>

            //Yêu cầu đổi mật khẩu thất bại
            //<editor-fold defaultstate="collapsed" desc="CHANGE_PASS_FAIL">
            case (Command.CHANGE_PASS_FAIL):
                JOptionPane.showMessageDialog(changepass, "Thay đổi mật khẩu thất bại", "Thông báo đổi password", JOptionPane.ERROR_MESSAGE);
                break;
            //</editor-fold>

            //Xóa bạn thành công   
            //<editor-fold defaultstate="collapsed" desc="DELETE_SUCCESS">
            case (Command.DELETE_SUCCESS):
                name = str.substring(3, sep);
                friend = str.substring(sep + 1, str.length());
                client.deleteFriend(friend);
                JOptionPane.showMessageDialog(client, "Đã xóa " + friend + " thành công", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>

            //Bạn đã xóa   
            //<editor-fold defaultstate="collapsed" desc="IS_DELETE">
            case (Command.IS_DELETE):
                friend = str.substring(3, str.length());
                client.deleteFriend(friend);
                break;
            //</editor-fold>

            //Lỗi xóa bạn            
            //<editor-fold defaultstate="collapsed" desc="DELETE_FAIL">
            case (Command.DELETE_FAIL):
                JOptionPane.showMessageDialog(client, "Lỗi xóa bạn", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
                break;
            //</editor-fold> 

            //Tạo nhóm thành công
            //<editor-fold defaultstate="collapsed" desc="CREATE_GROUP_SUCCESS">
            case(Command.CREATE_GROUP_SUCCESS):
                name=str.substring(3, sep);
                String group=str.substring(sep+1, str.length());
                client.modelGroup.addElement(group);
                groupchat=new frmGroupChat(group,client);
                groupchat.addMember(name);
                groupchat.setUsername(name);
                groupchat.setVisible(true); //hiện lên form chat nhóm 
                break;
            //</editor-fold>
                
            //Tạo nhóm thất bại
            //<editor-fold defaultstate="collapsed" desc="CREATE_GROUP_FAIL">
            case(Command.CREATE_GROUP_FAIL):
                name=str.substring(3, str.length());
                JOptionPane.showMessageDialog(client, "Tạo nhóm thất bại", "Lỗi tạo nhóm", JOptionPane.ERROR_MESSAGE);
                break;
            //</editor-fold>
                
            //đã tồn tại nhóm này
            //<editor-fold defaultstate="collapsed" desc="EXISTS_GROUP">
            case(Command.EXISTS_GROUP):
                group=str.substring(3, str.length());
                JOptionPane.showMessageDialog(client, "Nhóm đã tồn tại!", "Tạo nhóm thất bại", JOptionPane.ERROR_MESSAGE);
                break;
            //</editor-fold>
                
            //yêu cầu tham gia nhóm
            //<editor-fold defaultstate="collapsed" desc="HAVE_REQUEST_JOIN_GROUP">
            case(Command.HAVE_REQUEST_JOIN_GROUP):
                name = str.substring(3, sep);
                friend = str.substring(sep+1, sep1);
                group = str.substring(sep1+1, str.length());
                //Hiện thông báo mời tham gia nhóm của friend
                dialogButton = JOptionPane.YES_NO_OPTION;
                Object[] option = {"Tham gia", "Từ chối"};
                dialogResult = JOptionPane.showOptionDialog(client, "Bạn có lời mời tham gia vào nhóm chat từ "+ friend, "Nhóm chat của "+friend,dialogButton,
                        JOptionPane.QUESTION_MESSAGE,null,option,option[1]);
                if(dialogResult==0){
                    client.sendData(Command.JOIN_GROUP_ACCEPT+name+((char)001)+friend+((char)002)+group);
                    client.modelGroup.addElement(group);
                    groupchat=new frmGroupChat(group,client);
                    groupchat.setUsername(name);
                    groupchat.setVisible(true); //hiện lên form chat nhóm
                }
                break;
            //</editor-fold>
                
            //Thêm user mới vào nhóm
            //<editor-fold defaultstate="collapsed" desc="GROUP_ADD_NEW_USER">
            case(Command.GROUP_ADD_NEW_USER):
                name = str.substring(3, sep);
                friend = str.substring(sep+1, sep1);
                group = str.substring(sep1+1, str.length());
                groupchat.addMember(friend);
                break;
           //</editor-fold>
                
            //Cập nhật lại user
            //<editor-fold defaultstate="collapsed" desc="ADD_NEW_MEMBER">
            case(Command.ADD_NEW_MEMBER):
                name = str.substring(3, sep);
                friend = str.substring(sep+1, sep1);
                group = str.substring(sep1+1, str.length());
                groupchat.addMember(friend);
                break;
            //</editor-fold>
                
            //Hiển thị tin nhắn nhận được
            //<editor-fold defaultstate="collapsed" desc="MESSAGE_RECIEVED_GROUP">
            case (Command.MESSAGE_RECIEVED_GROUP):
                style = str.substring(3, 10);
                friend = str.substring(sep + 1, sep1);
                String gmsg = str.substring(sep1 + 1, str.length());
                groupchat.setVisible(true);
                groupchat.friendMessage(friend, style, gmsg);
                break;
            //</editor-fold>
                
            //Xóa ra khỏi nhóm chat
            //<editor-fold defaultstate="collapsed" desc="MESSAGE_RECIEVED_GROUP">
            case (Command.REMOVE_MEMBER_GROUP):
                String member = str.substring(3, str.length());
                groupchat.removeMember(member);
                break;
            //</editor-fold>
             
            //Đã tham gia nhóm
            //<editor-fold defaultstate="collapsed" desc="ALREADY_JOIN_GROUP">
            case (Command.ALREADY_JOIN_GROUP):
                name = str.substring(3, str.length());
                JOptionPane.showMessageDialog(null, "Bạn đã có tham gia nhóm chat rồi!","Thông báo",JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>
                
            //Đóng chat nhóm
            //<editor-fold defaultstate="collapsed" desc="REMOVE_GROUP">
            case (Command.REMOVE_GROUP):
                if(groupchat.isVisible()){
                    groupchat.dispose();                
                    JOptionPane.showMessageDialog(null,"Nhóm chat đã bị xóa","Thông báo",JOptionPane.INFORMATION_MESSAGE);
                }
                break;
            //</editor-fold>
                
            //Thông báo xóa nhóm
            //<editor-fold defaultstate="collapsed" desc="REMOVE_GROUP_SUCCESS">
            case (Command.REMOVE_GROUP_SUCCESS):
                JOptionPane.showMessageDialog(null,"Đã xóa nhóm thành công","Thông báo",JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>
                
            //Thông báo xóa nhóm
            //<editor-fold defaultstate="collapsed" desc="REMOVE_GROUP_SUCCESS">
            case (Command.REMOVE_GROUP_SUCCESS_AND_CLOSE):
                groupchat.dispose();
                JOptionPane.showMessageDialog(null,"Đã xóa nhóm thành công","Thông báo",JOptionPane.INFORMATION_MESSAGE);
                break;
            //</editor-fold>
              
            default:
                break;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar MenuBar;
    private javax.swing.ButtonGroup btg_MenuStatus;
    private javax.swing.ButtonGroup btg_RadioStatus;
    private javax.swing.JButton btn_Add;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JLabel lb_Hello;
    private javax.swing.JLabel lb_Username;
    private javax.swing.JList lst_Users;
    private javax.swing.JMenuItem mni_AddFriend;
    private javax.swing.JMenuItem mni_Address;
    private javax.swing.JMenuItem mni_Author;
    private javax.swing.JCheckBoxMenuItem mni_Busy;
    private javax.swing.JMenuItem mni_ChangePass;
    private javax.swing.JMenuItem mni_CreateGroup;
    private javax.swing.JMenuItem mni_DeleteFriend;
    private javax.swing.JMenuItem mni_DeleteGroup;
    private javax.swing.JMenuItem mni_Exit;
    private javax.swing.JMenuItem mni_FindFriend;
    private javax.swing.JCheckBoxMenuItem mni_Invisible;
    private javax.swing.JMenuItem mni_LockFriend;
    private javax.swing.JMenuItem mni_Logout;
    private javax.swing.JCheckBoxMenuItem mni_Online;
    private javax.swing.JMenuItem mni_SendMessage;
    private javax.swing.JCheckBoxMenuItem mni_ShowFriend;
    private javax.swing.JMenuItem mni_UnlockFriend;
    private javax.swing.JMenu mni_Users;
    private javax.swing.JRadioButton rad_Busy;
    private javax.swing.JRadioButton rad_Invisible;
    private javax.swing.JRadioButton rad_Online;
    private javax.swing.JScrollPane sp_users;
    private javax.swing.JTextField txt_Search;
    // End of variables declaration//GEN-END:variables

    private void searchListen() {
        txt_Search.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                filterList();
            }

            public void removeUpdate(DocumentEvent e) {
                filterList();
            }

            public void changedUpdate(DocumentEvent e) {
                filterList();
            }
        });
    }

    private void filterList() {
        modelShow.clear();
        String type = txt_Search.getText();
        if (type.equals("")) {
            updateFriendList();
        } else {
            for (int i = 0; i < modelSave.size(); i++) {
                if (modelSave.get(i).toString().toLowerCase().indexOf(type, 1) != -1 || modelSave.get(i).toString().toUpperCase().indexOf(type, 1) != -1) {
                    if (getStateUser(modelSave.getElementAt(i).toString()) != 5) {
                        modelShow.addElement(modelSave.getElementAt(i).toString());
                    }
                }
            }
        }
    }
}
